package com.example.ovulationperiodtracker.fragment;

import static android.view.View.VISIBLE;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.example.ovulationperiodtracker.databinding.FragmentStatistics2Binding;
import com.example.ovulationperiodtracker.utils.CycleAverageCalculator;
import com.example.ovulationperiodtracker.utils.MenstrualCycleCalculator;
import com.example.ovulationperiodtracker.utils.MyLog;
import com.example.ovulationperiodtracker.utils.SexStatisticsUtils;
import com.example.ovulationperiodtracker.view.NoteUtils;
import com.necer.base.BaseFragment;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class StatisticsFragment2 extends BaseFragment<FragmentStatistics2Binding> {


    @Override
    public FragmentStatistics2Binding createBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentStatistics2Binding.inflate(inflater);
    }

    @Override
    protected void onCreated() {


    }

    @Override
    public void onResume() {
        super.onResume();

        LocalDate lastJingStart = NoteUtils.getLastJingStart();
        if (lastJingStart == null) {
            return;
        }
        int periodLength = NoteUtils.getPeriodLength();
        binding.txtNextP1.setText(lastJingStart.plusDays(periodLength).toString());
        binding.dP1.setText(periodLength + "天");
        binding.txtNextP2.setText(lastJingStart.plusDays(periodLength * 2).toString());
        binding.dP2.setText(periodLength + "天");
        binding.txtNextP3.setText(lastJingStart.plusDays(periodLength * 3).toString());
        binding.dP3.setText(periodLength + "天");
        binding.txtNextP4.setText(lastJingStart.plusDays(periodLength * 4).toString());
        binding.dP4.setText(periodLength + "天");

        List<LocalDate> allJingStart = NoteUtils.getAllJingStart();


        if (allJingStart.size() == 1) {
            binding.rl1.setVisibility(VISIBLE);
            binding.txtPrevP1.setText(allJingStart.get(0).toString());
            long dayCount1 = NoteUtils.getPeriodLength();
            binding.dN1.setText(dayCount1 + "天");
        } else {
            if (allJingStart.size() > 1) {
                binding.rl1.setVisibility(VISIBLE);
                binding.txtPrevP1.setText(allJingStart.get(0).toString());
                long dayCount1 = ChronoUnit.DAYS.between(allJingStart.get(0), allJingStart.get(1));
                binding.dN1.setText(Math.abs(dayCount1) + "天");
            }
            if (allJingStart.size() > 2) {
                binding.rl2.setVisibility(VISIBLE);
                binding.txtPrevP2.setText(allJingStart.get(1).toString());
                long dayCount2 = ChronoUnit.DAYS.between(allJingStart.get(1), allJingStart.get(2));
                binding.dN2.setText(Math.abs(dayCount2) + "天");
            }
            if (allJingStart.size() > 3) {
                binding.rl3.setVisibility(VISIBLE);
                binding.txtPrevP3.setText(allJingStart.get(2).toString());
                long dayCount3 = ChronoUnit.DAYS.between(allJingStart.get(2), allJingStart.get(3));
                binding.dN3.setText(Math.abs(dayCount3) + "天");
            }

            if (allJingStart.size() > 4) {
                binding.rl4.setVisibility(VISIBLE);
                binding.txtPrevP4.setText(allJingStart.get(3).toString());
                long dayCount4 = ChronoUnit.DAYS.between(allJingStart.get(3), allJingStart.get(4));
                binding.dN4.setText(Math.abs(dayCount4) + "天");
            }
        }
        binding.dFC.setText(allJingStart.get(allJingStart.size() - 1).toString());
        binding.dLC.setText(allJingStart.get(0).toString());

        binding.avgLast3m.setText(SexStatisticsUtils.getAvgperiodLength(3) + "天");
        binding.avgLast6m.setText(SexStatisticsUtils.getAvgperiodLength(6) + "天");
        binding.avgLast12m.setText(SexStatisticsUtils.getAvgperiodLength(12) + "天");
        binding.avgLastAllData.setText(SexStatisticsUtils.getAvgperiodLength(-1) + "天");

        int[] ints = SexStatisticsUtils.calculateMaxMinInterval(12);
        //12个月周期最短
        binding.dSC.setText(ints[0] + "天");
        //12个月周期最长
        binding.dSL.setText(ints[1] + "天");
        //平均经期长度
        binding.aML.setText(SexStatisticsUtils.getAvgJingLength() + "天");


        binding.avgSexLastWeek.setText(SexStatisticsUtils.getWeekAveSex());
        binding.avgSexLastMonth.setText(SexStatisticsUtils.getMonthAveSex());
        binding.avgLastAll.setText(SexStatisticsUtils.allSexCount() + "");

        binding.avgOrgLastWeek.setText(SexStatisticsUtils.getWeekAveGao());
        binding.avgOrgLastMonth.setText(SexStatisticsUtils.getMonthAveGao());
        binding.avgLastOrgAll.setText(SexStatisticsUtils.allGaoCount() + "");

        String[] yiYunWeekAve = SexStatisticsUtils.getYiYunWeekAve();
        binding.avgSexLastWeekActive.setText(yiYunWeekAve[0]);
        binding.avgOrgLastWeekActive.setText(yiYunWeekAve[1]);

        String[] yiYunMonthAve = SexStatisticsUtils.getYiYunMonthAve();
        binding.avgSexLastMonthActive.setText(yiYunMonthAve[0]);
        binding.avgOrgLastMonthActive.setText(yiYunMonthAve[1]);

        binding.probOrg.setText(SexStatisticsUtils.getGaoSex());

    }


}
